<?php
class ViewAction extends MenuAction{
	/**
	 * @todo View all channel of a category
	 */
	public function category(){
		if(isset($_GET['caid']) && intval($_GET['caid'])){
			$category_id=$_GET['caid'];
			$Channel = M("Channel");
			$Content = M("Content");
			$ContentThumb = M("ContentThumb");
			$channels = $Channel->where('channel_category='.$category_id)->select();
			$thumbs = array();
			foreach ($channels as $key => $achannel) {
				$info= $Content->where('channel_id='.$achannel['channel_id'] .' AND content_state=1')->order('content_time desc')
				->limit('10')->select();
				//图片
				$imgs = $Content->where('channel_id='.$achannel['channel_id'].' and content_isthumb=1' .' AND content_state=1')->order('content_time desc')->limit('5')->select();
				if($imgs){
					foreach ($imgs as $img) {
						$thumb = $ContentThumb->where('thumb_content='.$img['content_id'])->find();
						$thumbtitle = $img['content_title'];
						$thumbs[] = array($thumb,$thumbtitle);
					}
				}else{
					$thumbs = array();
				}
				//End of img
				$array = array($achannel['channel_id'],$achannel['channel_name'],$thumbs,$info);
				unset($thumbs);
				$infos[] = $array;
			}
			/*$imgs = $Content->where('category_id='.$category_id.' and content_isthumb=1')->order('content_time desc')->limit('10')->select();
			 if($imgs){
				foreach ($imgs as $img) {
				$thumbs[] = $ContentThumb->where('thumb_content='.$img['content_id'])->find();
				}
				}else{
				$thumbs = array();
				}*/
			//get the hots point
			$hots = $Content->where('content_state=1')->order('content_click_count desc')->limit('11')->select();
			$mostnews = $Content->where('content_state=1')->order('content_time desc')->limit('11')->select();
			$this->assign('mostnews',$mostnews);
			$this->assign('hots',$hots);
			//$this->assign('thumbs',$thumbs);
			$this->assign('infos',$infos);
			$this->display();
		}else{
			$this->redirect('Index/index', '',0,'');
		}
	}
	/**
	 * @todo View all content of a channel
	 */
	public function channel(){
		if (isset($_GET["chid"]) && intval($_GET['chid'])) {
			$channel_id=$_GET['chid'];
			$Channel = M("Channel");
			$Content = M("Content");
			$ContentThumb = M("ContentThumb");
			$channels = $Channel->where('channel_id='.$channel_id)->find();
			$allcontent = $Content->where('channel_id='.$channel_id .' AND content_state=1')->count();
			//每页显示的个数
			if(isset($_GET['page'])  && intval($_GET['page'])){
				$nowpage=$_GET['page'];
			}else{
				$nowpage = 1;
			}
			//get the page
			$number = 20;
			$count = ceil($allcontent/$number);
			$low = ($nowpage-1) * $number;
			$info = $Content->where('channel_id='.$channel_id .' AND content_state=1')->order('content_time desc')
			->limit($low.','.$number)->select();
			$down = $nowpage - 4;
			if($down<1)
			$down = 1;
			$up = $nowpage + 4;
			if($up > $count)
			$up = $count;
			$middle = range($down, $up);
			//get the hot points
			$hots = $Content->where('content_state=1')->order('content_click_count desc')->limit('8')->select();
			$mostnews = $Content->where('content_state=1')->order('content_time desc')->limit('8')->select();
			//get the thumb pics
			$imgs = $Content->where('channel_id='.$channel_id.' and content_isthumb=1' .' AND content_state=1')
					->order('content_time desc')->limit('5')->select();
			$thumbs = array();
			if(imgs){
			foreach ($imgs as $key => $img) {
				$thumb = $ContentThumb->where('thumb_content='.$img['content_id'])->find();
				$thumbtitle = $img['content_title'];
				$thumbs[] = array($thumb,$thumbtitle);
			}
			}
			$this->assign('thumbs',$thumbs);
			$this->assign('mostnews',$mostnews);
			$this->assign('hots',$hots);
			$this->assign('pagecount',$count);
			$this->assign('middle',$middle);
			$this->assign('nowpage',$nowpage);
			$this->assign('info',$info);
			$this->assign('channel',$channels);
			$this->display();
		}else{
			$this->redirect('Index/index', '',0,'');
		}
	}
	/**
	 * @todo View a content by coid
	 */
	public function content(){
		if (isset($_GET["coid"]) && intval($_GET['coid'])) {
			$content_id=$_GET['coid'];
			$Channel = M("Channel");
			$Content = M("Content");
			$ContentAttach = M("ContentAttachment");
			$info = $Content->where('content_id='.$content_id .' AND content_state=1')->find();
			$data['content_id'] = $info['content_id'];
			$data['content_click_count'] = $info['content_click_count']+1;
			$Content->data($data)->save();
			$info['content_text']=str_replace("/Public",C('WWW')."Public",$info['content_text']);
			$channel = $Channel->where('channel_id='.$info['channel_id'])->find();
			if($info['content_attach']==1){
				$attachs = $ContentAttach->where('content_id='.$content_id)->select();
				$this->assign('attachs',$attachs);
			}
			if($info['content_albums'] == 1){
				$ContentImage = M("ContentImage");
				$albums = $ContentImage->where('image_content_id='.$content_id)->select();
				foreach ($albums as $key => $album) {
					$image[] = $album['image_name'];
					$image[] = str_replace('/albums_','/', $album['image_name']);
					$images[] = $image;
					unset($image);
				}
				$this->assign('albums',$images);
			}
			$this->assign('info',$info);
			$this->assign('channel',$channel);
			$this->display();;
		}else{
			$this->redirect('Index/index', '',0,'');
		};
	}
	/**
	 * @todo Show a stdic page
	 */
	public function page(){
		$Page = M('Page');
		if(isset($_GET['pageid']) && intval($_GET['pageid'])){
			$pageinfo = $Page->where('page_id='.$_GET['pageid'])->find();
			$this->assign('pageinfo',$pageinfo);
			$this->display();
		}else{
			$this->assign('waiSecond',10);
			$this->assign('jumpUrl','../Index/index');
			$this->error('请求的网页不存在');
		}
	}
	/**
	 * @todo Download a attachment
	 */
	public function download(){
		$attach_id = $_GET['attach'];
		$Attach = M('ContentAttachment');
		$file = $Attach->where('attachment_id='.$attach_id)->find();
		$filepath = '__APP__/../Public/Uploads/attach/'.$file['attachment_newname'];
		$filename = $file['attachment_name'];
		if($filename!='')
		$this->_download($filepath, $filename);
		else
		echo "File not exits";
	}
	/**
	 * $file_path file's path include it's filename
	 * $file_name file's name include it's extension
	 * Enter description here ...
	 * @param String $file_path
	 * @param String $file_name
	 */
	protected function _download($file_path, $file_name)
	{
		$file_path = chop($file_path); // 去掉路径中多余的空格.
			
		// 判断要下载的文件是否存在.
		if(!file_exists($file_path)) {
			echo "The file <";
			echo $file_name;
			echo "> not exits。";
			return false;
		}
		$file_size = filesize($file_path);

		header("Content-type:application/octet-stream");
		header("Accept-Ranges:bytes");
		header("Accept-Length:".$file_size);
		header("Content-Transfer-Encoding: binary");
		// 以真实文件名提供给浏览器下载.
		header("Content-Disposition: attachment; filename=".$file_name);
		header("Pragma: no-cache");
			
		$fp = fopen($file_path, "rb");
		$buffer_size = 1024;
		$cur_pos = 0;
			
		while(!feof($fp) &&
		($file_size - $cur_pos) > $buffer_size) {
			$buffer = fread($fp, $buffer_size);
			echo $buffer;
			$cur_pos += $buffer_size;
		}
			
		$buffer = fread($fp, $file_size - $cur_pos);
		echo $buffer;
		fclose($fp);

		return true;
	}
}
?>